package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.dto.resp.R;
import com.itheima.reggie.pojo.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;

@RestController
@Slf4j
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    //登录方法
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //1、将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //判断页面输入的名字在数据库是否存在
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        //获取一个对象
        Employee oneEmployee = employeeService.getOne(wrapper);
        //判断用户名是否存在
        if (oneEmployee == null) {//不存在的情况
            return R.error("用户名不存在,登录失败");
        }
        //用户存在,判断密码是否正确
        //密码不正确
        if (!oneEmployee.getPassword().equals(password)) {
            return R.error("密码不正确,登录失败");
        }
        //密码正确,判断状态情况
        if (oneEmployee.getStatus() == 0) {//禁用状态
            return R.error("账号已禁用,登录失败");
        }
        //登录正确,将员工id存到session中
        //使用request对象的getSession()获取session,将数据存储到session中
        request.getSession().setAttribute("employee", oneEmployee.getId());
        //返回正确登录结果
        return R.success(oneEmployee);
    }

    //退出方法
    @PostMapping("/logout")
    public R logout(HttpServletRequest request) {
        //清空session
        //方式一
        request.getSession().removeAttribute("employee");
        /*方式二
        request.getSession().invalidate();*/
        //返回信息
        return R.success("退出成功");
    }

    //新增员工
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("新增员工,员工信息:{}", employee.toString());
        //员工中的数据,哪些是需要处理的
        //设置初始密码123456,需要进行md5加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //添加时间  为当前系统时间
       // employee.setCreateTime(LocalDateTime.now());
        //更新时间  为当前系统时间
       // employee.setUpdateTime(LocalDateTime.now());
        //create_user 和 update_user应为当前的登录人
        //先获取当前用户的id
       // Long id = (Long) request.getSession().getAttribute("employee");
        //employee.setCreateUser(id);
        //employee.setUpdateUser(id);
        //调用业务层的save方法
        boolean save = employeeService.save(employee);
        if (save) {
            //返回结果R
            return R.success("员工添加成功！");
        }
     return R.error("员工添加失败");

    }

    //分页查询
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        //打印前端传送的数据
        log.info("page:{},pageSize:{},name:{}", page, pageSize, name);
        //分页构造器
        Page pageInfo = new Page(page, pageSize);
        //条件构造器
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        //模糊查询,添加过滤条件
        wrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        //或者 wrapper.like(name!=null && name!="",Employee::getName,name);
        //执行查询
        employeeService.page(pageInfo, wrapper);
        return R.success(pageInfo);
    }

    //员工的禁用和启用
    //根据id修改信息
    @PutMapping
    public R update(HttpServletRequest request, @RequestBody Employee employee) {
        log.info(employee.toString());
        //获取当前登录用户的id
//        Long empId = (Long) request.getSession().getAttribute("employee");
//        //更新是谁更新用户
//        employee.setUpdateUser(empId);
//        //更新当前更新系统的时间
//        employee.setUpdateTime(LocalDateTime.now());
        //调用service中方法
        employeeService.updateById(employee);
        //修改成功,返回数据
        return R.success("员工信息修改成功");
    }

    //回显数据 根据id查询信息
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id) {
        log.info("根据id查询当前信息......");
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            return R.success(employee);
        }
        return R.error("没有该员工信息");
    }

}
